package com.lishe.lishe.lishe.module.excitation.presenter;

import android.util.Log;

import com.lishe.lishe.lishe.bean.OrderObject;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.bean.BannerObject;
import com.lishe.lishe.lishe.module.excitation.bean.EncourageObject;
import com.lishe.lishe.lishe.module.excitation.bean.HeadInfo;
import com.lishe.lishe.lishe.module.excitation.bean.MyMessageObject;
import com.lishe.lishe.lishe.module.excitation.bean.WelfareMessageObject;
import com.lishe.lishe.lishe.module.excitation.bean.WelfareObject;
import com.lishe.lishe.lishe.module.excitation.bean.WelfareOrderObject;
import com.lishe.lishe.lishe.module.excitation.bean.admire.AcceptGiftObject;
import com.lishe.lishe.lishe.module.excitation.bean.company.CompanyInfo;
import com.lishe.lishe.lishe.module.excitation.bean.company.JoinCompany;
import com.lishe.lishe.lishe.module.excitation.bean.company.SearchCompany;
import com.lishe.lishe.retrofit.bean.InfoArrayObject;
import com.lishe.lishe.lishe.module.excitation.bean.admire.AdmireObject;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserInfoObject;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;
import com.lishe.lishe.lishe.utils.NetWorkUtils;
import com.lishe.lishe.retrofit.BaseDataPageBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.lishe.module.excitation.interfaces.ExcitationService;
import com.lishe.lishe.lishe.utils.StringUtils;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.MyObserver.FileUploadObserver;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.lishe.lishe.retrofit.bean.DataArrayObject;
import com.lishe.lishe.retrofit.bean.InfoObject;
import com.lishe.lishe.retrofit.bean.UploadInfo;
import com.lishe.lishe.retrofit.presenter.BaseInfoBody;
import com.lishe.lishe.retrofit.presenter.FilePresenter;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by DELL on 2017/11/21.
 */

public class ExcitationPresenter {

    private static ExcitationPresenter mExcitation;

    public static ExcitationPresenter getInstance() {
        if (mExcitation == null) {
            mExcitation = new ExcitationPresenter();
        }

        return mExcitation;
    }

    public void getUserInfo(String token, final DataResponseListener<UserObject> listener) throws Exception {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getUserInfo(token, sign).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                })
                .subscribe(new Observer<BaseRequestBody<UserInfoObject>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<UserInfoObject> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData().getUserObject());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 上传数据流并更改用户头像信息
     *
     * @param buff       图片字符流
     * @param token
     * @param userObject
     * @param listener
     */
    public void updateUserInfo(byte[] buff, String token, UserObject userObject, DataResponseListener<String> listener) throws Exception {
        uploadAndUpdateUserHead(null, buff, token, userObject, listener);
    }

    /**
     * 上传文件并更改用户头像信息
     *
     * @param file
     * @param token
     * @param userObject
     * @param listener
     */
    public void updateUserInfo(File file, String token, UserObject userObject, DataResponseListener<String> listener) throws Exception {
        uploadAndUpdateUserHead(file, null, token, userObject, listener);
    }

    /**
     * file 和 buff 二选一
     *
     * @param file
     * @param buff
     * @param token
     * @param userObject
     * @param listener
     */
    private void uploadAndUpdateUserHead(File file, byte[] buff, final String token, final UserObject userObject,
                                         final DataResponseListener<String> listener) throws Exception {
        final ExcitationService excitationService = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);

        FilePresenter.getInstance().upload(file, buff, token, new FileUploadObserver<BaseRequestBody<UploadInfo>>() {
            @Override
            protected void onProgress(int bytesWritten, int contentLength) {
                //运行在子线程
                Log.e("TAG", "  onProgress    bytesWritten =" + bytesWritten + "  contentLength  = " + contentLength);
            }

            @Override
            protected void onUpLoadFail(Throwable e) {
                listener.onUploadFail(e.toString());
            }

            @Override
            protected void onUpLoadSuccess(BaseRequestBody<UploadInfo> requestBody) {
                listener.onUploadSuccess();
                if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                    userObject.setUserAvatarPath(requestBody.getData().getInfo());
                    Map<String, String> signParams = new HashMap<>();
                    signParams.put("token", token);
                    signParams.put("userAvatarPath", userObject.getUserAvatarPath());
                    String sign = StringUtils.createSignString(signParams) + Globals.key;
                    sign = StringUtils.md5(sign);
                    excitationService.updateUserHeadImage(token,
                            userObject.getUserAvatarPath(), sign)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<BaseRequestBody<UploadInfo>>() {
                                @Override
                                public void onSubscribe(@NonNull Disposable d) {

                                }

                                @Override
                                public void onNext(@NonNull BaseRequestBody<UploadInfo> requestBody) {
                                    if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                                        listener.onSuccess(requestBody.getData().getInfo());
                                    } else {
                                        listener.onFail(requestBody.getMsg());
                                    }
                                }

                                @Override
                                public void onError(@NonNull Throwable e) {
                                    listener.onError(e);
                                }

                                @Override
                                public void onComplete() {

                                }
                            });
                }
            }

            @Override
            protected void onUpLoadStart() {
                listener.onPrepare();
            }
        });
    }

    /**
     * 更改性别
     *
     * @param token
     * @param gender
     * @param listener
     */
    public void updatePersonGender(String token, final String gender, final DataResponseListener<String> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("gender", gender);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        RetrofitFactory.createExcitationUrl().create(ExcitationService.class).updateUserGender(token, gender, sign)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<UploadInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<UploadInfo> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(gender);
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 更改用户名
     *
     * @param token
     * @param name
     * @param listener
     */
    public void updatePersonName(String token, final String name, final DataResponseListener<String> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("empName", name);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        RetrofitFactory.createExcitationUrl().create(ExcitationService.class).updateUserName(token, name, sign)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<UploadInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<UploadInfo> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(name);
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 更改用户名
     *
     * @param token
     * @param email
     * @param listener
     */
    public void updatePersonEmail(String token, final String email, final DataResponseListener<String> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("empEmail", email);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        RetrofitFactory.createExcitationUrl().create(ExcitationService.class).updateUserEmail(token, email, sign)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<UploadInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<UploadInfo> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(email);
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取福利列表
     *
     * @param token
     * @param pageNo
     * @param pageSize
     * @param listener
     * @throws Exception
     */
    public void getUserWelfareList(String token, String pageNo, String pageSize, final DataResponseListener<BaseDataPageBody> listener) throws Exception {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("pageNo", pageNo);
        signParams.put("pageSize", pageSize);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService excitationService = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        excitationService.getUserWelfareList(token, pageNo, pageSize, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribe(new Observer<BaseRequestBody<BaseDataPageBody<WelfareObject>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseRequestBody<BaseDataPageBody<WelfareObject>> baseDataPageBodyBaseRequestBody) {
                if (baseDataPageBodyBaseRequestBody.getResult() == 100 && baseDataPageBodyBaseRequestBody.getErrcode() == 0) {
                    listener.onSuccess(baseDataPageBodyBaseRequestBody.getData());
                } else {
                    listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                listener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    /**
     * 获取福利详情
     */
    public void getWelfareDetail(String token, String empId, final DataResponseListener<WelfareObject> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("empId", empId);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getWelfareDetail(token, empId, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseInfoBody<WelfareObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseInfoBody<WelfareObject>> welfareObjectBaseRequestBody) {
                        if (welfareObjectBaseRequestBody.getErrcode() == 0 && welfareObjectBaseRequestBody.getResult() == 100) {
                            listener.onSuccess(welfareObjectBaseRequestBody.getData().getInfo());
                        } else {
                            listener.onFail(welfareObjectBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getOrderStatusList(String token, String orderNo, final DataResponseListener<BaseDataPageBody> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("orderNo", orderNo);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getOrderStatusList(token, orderNo, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseDataPageBody<WelfareOrderObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseDataPageBody<WelfareOrderObject>> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getErrcode() == 0 && baseDataPageBodyBaseRequestBody.getResult() == 100) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取同事头条列表
     *
     * @param token
     */
    public void getHeadInfoList(String token, final DataResponseListener<BaseDataPageBody> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getAppHeadList(token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseDataPageBody<HeadInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseDataPageBody<HeadInfo>> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getErrcode() == 0 && baseDataPageBodyBaseRequestBody.getResult() == 100) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取团队激励列表
     *
     * @param token
     * @param listener
     */
    public void getTeamEncourageList(String token, final DataResponseListener<BaseDataPageBody> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getAppEncourageList(token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseDataPageBody<EncourageObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseDataPageBody<EncourageObject>> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getErrcode() == 0 && baseDataPageBodyBaseRequestBody.getResult() == 100) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取收到鼓励列表
     *
     * @param token
     * @param eventId
     * @param listener
     */
    public void getAdmireList(String token, final String eventId, final DataResponseListener<List<AdmireObject>> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("eventId", eventId);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        RetrofitFactory.createExcitationUrl().create(ExcitationService.class).getAdmireList(token, eventId, sign)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoArrayObject<AdmireObject>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<InfoArrayObject<AdmireObject>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            if (requestBody.getData() == null) {
                                listener.onDataEmpty();
                            } else {
                                listener.onSuccess(requestBody.getData().getInfo());
                            }
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 更改用户名
     *
     * @param token
     * @param eventId
     * @param listener
     */
    public void getUserAcceptGiftList(String token, final String eventId, final DataResponseListener<List<AcceptGiftObject>> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("eventId", eventId);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        RetrofitFactory.createExcitationUrl().create(ExcitationService.class).getUserAcceptGiftList(token, eventId, sign)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoArrayObject<AcceptGiftObject>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<InfoArrayObject<AcceptGiftObject>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            if (requestBody.getData() == null) {
                                listener.onDataEmpty();
                            } else {
                                listener.onSuccess(requestBody.getData().getInfo());
                            }
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取点赞消息列表
     */
    public void getMessageList(String token, final DataResponseListener<BaseDataPageBody> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getMessageList(token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseDataPageBody<MyMessageObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseDataPageBody<MyMessageObject>> baseDataPageBodyBaseRequestBody) {
                        if (baseDataPageBodyBaseRequestBody.getResult() == 100 && baseDataPageBodyBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseDataPageBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseDataPageBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 点赞墙的banner
     */
    public void getExcitionBanner(String token, final DataResponseListener<DataArrayObject> listener) {
        ExcitationService service = RetrofitFactory.create().create(ExcitationService.class);
        service.getExcitionBanner(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DataArrayObject<BannerObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DataArrayObject<BannerObject> bannerObjectDataArrayObject) {
                        if (bannerObjectDataArrayObject.getResult() == 100 && bannerObjectDataArrayObject.getErrcode() == 0) {
                            listener.onSuccess(bannerObjectDataArrayObject);
                        } else {
                            listener.onFail(bannerObjectDataArrayObject.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 搜索企业
     */
    private Disposable sendDisposable;
    public void getComList(String token, String comName, final DataResponseListener<List<SearchCompany>> listener) {
        if(sendDisposable!=null){
            sendDisposable.dispose();
        }
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("comName", comName);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getComList(token, comName, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoArrayObject<SearchCompany>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        sendDisposable=d;
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(BaseRequestBody<InfoArrayObject<SearchCompany>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            if (requestBody.getData() == null) {
                                listener.onDataEmpty();
                            } else {
                                listener.onSuccess(requestBody.getData().getInfo());
                            }
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 加入企业
     */
    public void joinCom(String token, String comId, String empName, final DataResponseListener<JoinCompany> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("comId", comId);
        signParams.put("empName", empName);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);

        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.joinCom(token, comId, empName, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoObject<JoinCompany>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(BaseRequestBody<InfoObject<JoinCompany>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData().getInfo());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取企业首页的福利消息
     *
     * @param token
     * @param listener
     */
    public void getAppWelfareList(String token, final DataResponseListener<BaseDataPageBody> listener) {

        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getAppWelfareList(token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseDataPageBody<WelfareMessageObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseDataPageBody<WelfareMessageObject>> baseInfoBodyBaseRequestBody) {
                        if (baseInfoBodyBaseRequestBody.getResult() == 100 && baseInfoBodyBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseInfoBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseInfoBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取企业首页的福利消息
     *
     * @param token
     * @param listener
     */
    public void getComConfig(String token, final DataResponseListener<BaseInfoBody> listener) {

        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getComConfig(token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<BaseInfoBody<CompanyInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<BaseInfoBody<CompanyInfo>> baseInfoBodyBaseRequestBody) {
                        if (baseInfoBodyBaseRequestBody.getResult() == 100 && baseInfoBodyBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseInfoBodyBaseRequestBody.getData());
                        } else {
                            listener.onFail(baseInfoBodyBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    /**
     * 点赞墙消息阅读状态变更
     *
     * @param token
     * @param listener
     */
    public void updateNewsStatus(String id, String token, final DataResponseListener<BaseRequestBody> listener) {

        Map<String, String> signParams = new HashMap<>();
        signParams.put("id", id);
        signParams.put("token", token);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.updateNewsStatus(id, token, sign)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody baseRequestBody) {
                        if (baseRequestBody.getResult() == 100 && baseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseRequestBody);
                        } else {
                            listener.onFail(baseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * //获取用户所在所有企业列表
     *
     * @param token
     * @param listener
     */
    public void getUserComAll(String token, final DataResponseListener<InfoArrayObject> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getUserComAll(token, sign).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoArrayObject<CompanyInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<InfoArrayObject<CompanyInfo>> baseRequestBody) {
                        if (baseRequestBody.getResult() == 100 && baseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseRequestBody.getData());
                        } else {
                            listener.onFail(baseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 员工离职/退出企业  类型： 1：离职，2：退出
     */
    public void userQuitCom(String token, String type, String comId, final DataResponseListener<String> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("type", type);
        signParams.put("comId", comId);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.userQuitCom(token, sign, comId, type).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody baseRequestBody) {
                        if (baseRequestBody.getResult() == 100 && baseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(baseRequestBody.getMsg());
                        } else {
                            listener.onFail(baseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取企业和员工信息
     */
    public void getUserAndComInfo(String token, String comId, final DataResponseListener<UserObject> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        signParams.put("comId", comId);

        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getUserAndComInfo(token, sign, comId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoObject<UserObject>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<InfoObject<UserObject>> infoObjectBaseRequestBody) {
                        if (infoObjectBaseRequestBody.getResult() == 100 && infoObjectBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(infoObjectBaseRequestBody.getData().getInfo());
                        } else {
                            listener.onFail(infoObjectBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    /**
     * 获取企业和员工信息
     */
    public void getComSize(String token, final DataResponseListener<Integer> listener) {
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        ExcitationService service = RetrofitFactory.createExcitationUrl().create(ExcitationService.class);
        service.getComSize(token, sign).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<InfoObject<Integer>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<InfoObject<Integer>> infoObjectBaseRequestBody) {
                        if (infoObjectBaseRequestBody.getResult() == 100 && infoObjectBaseRequestBody.getErrcode() == 0) {
                            listener.onSuccess(infoObjectBaseRequestBody.getData().getInfo());
                        } else {
                            listener.onFail(infoObjectBaseRequestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

